 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Demo3Layer_Payment.Data_Access_Layer
{
    class DataProvider
    {
        #region "Attribute"
        private SqlConnection objConnection;
        private SqlCommand objCommand;
        private string strError;
        #endregion

        #region"Constructor method"
        /// <summary>
        /// Constructor method object DataProvider' class
        /// </summary>
        public DataProvider()
        {
            this.objConnection = new SqlConnection();
            this.objCommand = new SqlCommand();
            this.strError = string.Empty;
        }
        #endregion

        #region "Method"
        /// <summary>
        /// Method test state connect to database:true: connect, fasle: not connect
        /// </summary>
        /// <returns></returns>
        public bool IsConnect()
        {
            if (string.Compare(this.objConnection.State.ToString(), "Open") == 0)
            //if(this.objConnection.State==ConnectionState.Open)
                return true;
            return false;
        }
        /// <summary>
        /// Method connect to database
        /// </summary>
        public bool Connect()
        {
            string strConnectionString = "";
            //string strConnectionString = "";
            try
            {
                this.objConnection = new SqlConnection(strConnectionString);
                this.objConnection.Open();
            }
            catch (Exception ex)
            {
                //this.strError = ex.Message;
                throw ex;
                return false;
            }
            return true;
        }
        /// <summary>
        /// Method stop connect to database
        /// </summary>
        public void DeConnect()
        {
            if (this.objConnection != null)
            {
                this.objConnection.Close();
                this.objConnection.Dispose();
                this.objConnection = null;
            }
        }
        /// <summary>
        /// Method create a store procedure
        /// </summary>
        /// <param name="strStoreProcedureName"></param>
        public void CreateStoreProcedure(string strStoreProcedureName)
        {
            //if strStoreProcedureName==null-> what happen?
            this.objCommand = new SqlCommand(strStoreProcedureName,this.objConnection);
            this.objCommand.CommandText = strStoreProcedureName;
            this.objCommand.CommandType = CommandType.StoredProcedure;
            this.objCommand.CommandTimeout = 10;
        }
        /// <summary>
        /// Method ExecuteNonQuery
        /// </summary>
        /// <returns>intnum</returns>
        public int ExecuteStoreProcedureNonQuery()
        {
            int intnum;
            try
            {
                intnum = this.objCommand.ExecuteNonQuery();
            }
            catch(SqlException ex)
            {
                throw ex;
            }
            return intnum;
        }
        /// <summary>
        /// Method add parameter for object sqlCommand
        /// </summary>
        /// <param name="strParameterName"></param>
        /// <param name="objValues"></param>
        public void AddParameter(string strParameterName, Object objValues)
        {
            this.objCommand.Parameters.AddWithValue(strParameterName,objValues);
        }
        /// <summary>
        /// Method execute store procdure to datatable
        /// </summary>
        /// <returns>objtable</returns>
        public DataTable ExecuteStoreProcdureToDataTable()
        {
            DataTable objtable;
            SqlDataAdapter objadapter = new SqlDataAdapter(this.objCommand);
            DataSet objdataSet = new DataSet();
            try
            {
                objadapter.Fill(objdataSet);
                objtable = objdataSet.Tables[0];
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return objtable;
        }
        #endregion
    }
}
